package com.tanhua.server.service;

import com.tanhua.autoconfig.template.AipFaceTemplate;
import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.dubbo.api.RecommendUserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.UserLikeApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.*;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserInfoService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private OssTemplate ossTemplate;

    @Autowired
    private AipFaceTemplate aipFaceTemplate;

    @DubboReference
    private UserLikeApi userLikeApi;

    @DubboReference
    private RecommendUserApi recommendUserApi;

    public void save(UserInfo userInfo) {
        userInfoApi.save(userInfo);
    }

    //更新用户头像
    public void updateHead(MultipartFile headPhoto, Long id) throws IOException {
        //1、将图片上传到阿里云oss
        String imageUrl = ossTemplate.upload(headPhoto.getOriginalFilename(), headPhoto.getInputStream());
        //2、调用百度云判断是否包含人脸
        boolean detect = aipFaceTemplate.detect(imageUrl);
        //2.1 如果不包含人脸，抛出异常
        if(!detect) {
            throw new BusinessException(ErrorResult.faceError());
        }else{
            //2.2 包含人脸，调用API更新
            UserInfo userInfo = new UserInfo();
            userInfo.setId(id);
            userInfo.setAvatar(imageUrl);
            userInfoApi.update(userInfo);
        }
    }

    //根据id查寻
    public UserInfoVo findById(Long id) {
        UserInfo userInfo = userInfoApi.findById(id);

        UserInfoVo vo = new UserInfoVo();

        BeanUtils.copyProperties(userInfo,vo); //copy同名同类型的属性

        if(userInfo.getAge() != null) {
            vo.setAge(userInfo.getAge().toString());
        }

        return vo;
    }

    //更新
    public void update(UserInfo userInfo) {
        userInfoApi.update(userInfo);
    }

    public PageResult userLoves(Integer page, Integer pagesize) {
        List<UserLike> list = userLikeApi.userLoves(UserHolder.getUserId(), page, pagesize);
        List<UserLikeVo> userLikeVos = new ArrayList<>();
        list.stream().map(UserLike::getLikeUserId).forEach(c -> {
            UserInfo userInfo = userInfoApi.findById(c);
            UserLikeVo userLikeVo = new UserLikeVo();
            BeanUtils.copyProperties(userInfo, userLikeVo);
            userLikeVo.setAlreadyLove(true);
            RecommendUser recommendUser = recommendUserApi.findRecommendUser(c, UserHolder.getUserId());
            userLikeVo.setMatchRate(recommendUser.getScore().intValue());
            userLikeVos.add(userLikeVo);
        });

        return new PageResult(page, pagesize, Long.valueOf(list.size()), userLikeVos);
    }

    public PageResult lovesUser(Integer page, Integer pagesize) {
        List<UserLike> list = userLikeApi.lovesUser(UserHolder.getUserId(), page, pagesize);
        List<UserLikeVo> userLikeVos = new ArrayList<>();
        list.stream().map(UserLike::getUserId).forEach(c -> {
            UserInfo userInfo = userInfoApi.findById(c);
            UserLikeVo userLikeVo = new UserLikeVo();
            BeanUtils.copyProperties(userInfo, userLikeVo);
            UserLike userLike = userLikeApi.AlreadyLove(UserHolder.getUserId(), c);
            if (userLike == null) {
                userLikeVo.setAlreadyLove(false);
            } else {
                userLikeVo.setAlreadyLove(userLike.getIsLike());
            }

            RecommendUser recommendUser = recommendUserApi.findRecommendUser(UserHolder.getUserId(), c);
            userLikeVo.setMatchRate(recommendUser.getScore().intValue());

            userLikeVos.add(userLikeVo);
        });
        return new PageResult(page, pagesize, Long.valueOf(list.size()), userLikeVos);
    }


    public void modifyFansLoveStatus(Long uid) { //uid是粉丝的id
        userLikeApi.modifyFansLoveStatus(UserHolder.getUserId(), uid);
    }

    public void deleteLoveStatus(Long uid) {
        userLikeApi.deleteLoveStatus(UserHolder.getUserId(), uid);
    }


    public PageResult loveEach(Integer page, Integer pagesize) {
        List<UserLike> userLikes = userLikeApi.userLoves(UserHolder.getUserId(), page, pagesize);//我喜欢的人
        //找到我喜欢的人也喜欢我的
        ArrayList<UserLikeVo> userLikeVos = new ArrayList<>();
        for (UserLike userLike : userLikes) {
            Long girlId = userLike.getLikeUserId();
            List<UserLike> girlLikes = userLikeApi.userLoves(girlId, page, pagesize);
            girlLikes.stream().filter(
                    c -> c.getLikeUserId().equals(UserHolder.getUserId())  //这里面就是我喜欢的也喜欢我
            ).forEach(c -> {
                UserInfo userInfo = userInfoApi.findById(c.getUserId());
                UserLikeVo userLikeVo = new UserLikeVo();
                BeanUtils.copyProperties(userInfo, userLikeVo);
                userLikeVo.setAlreadyLove(true);
                RecommendUser recommendUser = recommendUserApi.findRecommendUser(userLike.getUserId(), UserHolder.getUserId());
                userLikeVo.setMatchRate(recommendUser.getScore().intValue());
                userLikeVos.add(userLikeVo);
            });
        }
        return new PageResult(page, pagesize, Long.valueOf(userLikeVos.size()), userLikeVos);
    }

    public CountsVo findCounts() {
        CountsVo vo=userLikeApi.findCounts(UserHolder.getUserId());
        return vo;
    }
}
